class Solution
{
public:
    int destroyTargets(vector<int> &nums, int space)
    {
        unordered_map<int, int> modCount;
        unordered_map<int, int> modMin;
        for (int num : nums)
        {
            int mod = num % space;
            ++modCount[mod];
            if (!modMin.count(mod))
            {
                modMin[mod] = num;
            }
            else
            {
                modMin[mod] = min(modMin[mod], num);
            }
        }
        int maxCount = 0;
        int minNum = INT_MAX;
        for (auto &[mod, count] : modCount)
        {
            if (count > maxCount)
            {
                maxCount = count;
                minNum = modMin[mod];
            }
            else if (count == maxCount)
            {
                minNum = min(minNum, modMin[mod]);
            }
        }
        return minNum;
    }
};